package com.hj.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;






import org.springframework.transaction.annotation.Transactional;

import com.hj.dao.SingleResetMapper;
import com.hj.entity.Cti_humanservice;
import com.hj.entity.Cti_ivrservice;
import com.hj.entity.Cti_routeservice;
import com.hj.entity.Ss_accesscode;
import com.hj.entity.Ss_accesscodecopy;
import com.hj.entity.Vcc_uptnum;
import com.hj.service.SingleResetService;
import com.hj.utils.MultipleDataSource;

@Service("singleResetService")
@Transactional(value="txManager")
public class SingleResetServiceImpl implements SingleResetService{
	@Autowired
	SingleResetMapper singleResetMapper;

	public String singleReset(String single400) {
		MultipleDataSource.setDataSourceKey("dataSource");
		String vccId=null,json=null;
		if(single400.length()>4){ //后面通过正则校验
			vccId=single400.substring(4);
		//查询400号码是否存在
		List<Ss_accesscode> exist=singleResetMapper.search_exist(vccId);
		if(exist.isEmpty()|exist==null){
			json="此400号码不存在!";
			return json;
		}else{
			//查询备份表接入策略
			MultipleDataSource.setDataSourceKey("dataSource1");
			singleResetMapper.update_cp(single400);//改状态
			List<Ss_accesscodecopy> s_acc=singleResetMapper.search_sacc(vccId);
			if(s_acc.isEmpty()|s_acc==null){
				//查询业务节点
				MultipleDataSource.setDataSourceKey("dataSource");
				Vcc_uptnum v_un=singleResetMapper.search_vun(single400);
				if(v_un!=null){
					//绑定业务节点
					singleResetMapper.update_un(vccId);
					return json="复机成功!";
				}else{
					Cti_routeservice c_rs=singleResetMapper.search_crs(vccId);
					if(c_rs!=null){
						//绑定路由
						singleResetMapper.update_rs(c_rs);
						singleResetMapper.update_cp(single400);//改状态
						return json="复机成功!";
					}else{
						Cti_ivrservice c_is=singleResetMapper.search_is(vccId);
						if(c_is!=null){
							//绑定ivr
							singleResetMapper.update_is(c_is);
							return json="复机成功!";
						}else{
							List<Cti_humanservice> c_hs=singleResetMapper.search_hs(vccId);
							if(!c_hs.isEmpty()|c_hs!=null){
								//绑定人工
								for(Cti_humanservice chs:c_hs){
								singleResetMapper.update_hs(chs);
								}
								return json="复机成功!";
							}
						}
					}
				}
			}else{
				MultipleDataSource.setDataSourceKey("dataSource");
				for(Ss_accesscodecopy sacc:s_acc){
				singleResetMapper.update_sacc(sacc);
				}
				return json="复机成功!";
			}
		}
		 return json="未找到该400号码的接入策略，请手动复机!";
		}else{
		 return json="请输入正确的400号码!";
		}
		
	}
	
}
